#include "removeuserwidget.h"
#include "ui_removeuserwidget.h"
#include "config.h"
RemoveUserWidget::RemoveUserWidget(QWidget *main,QWidget *login) :
    QWidget(nullptr),
    ui(new Ui::RemoveUserWidget)
{
    ui->setupUi(this);

    keyboard = new KeyBoard();
    this->login =login;
    this->main = main;
    ui->lineEdit_name->installEventFilter(keyboard);
    ui->lineEdit_pw->installEventFilter(keyboard);


}

RemoveUserWidget::~RemoveUserWidget()
{
    delete ui;
}

void RemoveUserWidget::getSocked()
{
    socket = new QTcpSocket;
    connect(socket, &QTcpSocket::readyRead, this, &RemoveUserWidget::recv_data);
    socket->connectToHost(SOCKET_SERVER_ADDRESS,SOCKET_SERVER_PORT);
}
void RemoveUserWidget::on_pushButton_remove_clicked()
{
    getSocked();
    char buf[20]="remove";
    char buf_name[20], buf_passwd[20];
    QString name = ui->lineEdit_name->text(); // 获取用户名
    QString passwd = ui->lineEdit_pw->text(); // 获取密码
    strcpy(buf_name, name.toLatin1().data()); // 将QString类型的用户名转换为char数组
    strcpy(buf_passwd, passwd.toLatin1().data()); // 将QString类型的密码转换为char数组
    // 发送用户名和密码给服务器
    socket->write(buf,sizeof(buf));
    socket->write(buf_name, sizeof(buf_name));
    socket->write(buf_passwd, sizeof(buf_passwd));
}
void RemoveUserWidget::recv_data()
{
    char buf[20];
    socket->read(buf, sizeof(buf)); // 读取服务器返回的数据
    if(strcmp(buf, "200") == 0) // 如果返回的数据为"200"
    {
        login->show();
        socket->close();
        main->close();
        this->close();
    }
    else
    {

        ui->lineEdit_recv->setText(buf); // 在界面上显示返回的数据
    }
    ui->lineEdit_pw->setText("");

}

void RemoveUserWidget::on_backButton_clicked()
{
    this->close();
    socket->close();
}

